﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GMUtilitiesDotNet.Services.ServiceInterfaces;
using System.Windows.Forms;
using System.Windows;
using System.Windows.Interop;

namespace GMUtilitiesDotNet.Services.DefaultServiceImplementations
{
    internal sealed class WPFSelectFolderService : ISelectFolderService
    {
        #region Properties
        #region InitialDirectory
        /// <summary>
        /// InitialDirectory property.
        /// </summary>
        public Environment.SpecialFolder InitialDirectory
        {
            get { return mFolderBrowserDialog.RootFolder; }
            set { mFolderBrowserDialog.RootFolder = value; }
        }
        #endregion

        #region Description
        /// <summary>
        /// Description property.
        /// </summary>
        public string Description
        {
            get { return mFolderBrowserDialog.Description; }
            set { mFolderBrowserDialog.Description = value; }
        }
        #endregion

        #region SelectedPath
        /// <summary>
        /// SelectedPath property.
        /// </summary>
        public string SelectedPath
        {
            get { return mFolderBrowserDialog.SelectedPath; }
        }
        #endregion

        private FolderBrowserDialog mFolderBrowserDialog;
        #endregion

        #region Constructors
        public WPFSelectFolderService()
        {
            mFolderBrowserDialog = new FolderBrowserDialog();
            mFolderBrowserDialog.ShowNewFolderButton = false;
        }
        #endregion

        #region Overriden Methods
        public bool ShowDialog(Window pOwner)
        {
            Wpf32Window wpf32Window = pOwner != null ? new Wpf32Window(pOwner) : null;
            DialogResult dialogResult = mFolderBrowserDialog.ShowDialog(wpf32Window);
            return dialogResult == DialogResult.OK;
        }
        #endregion

        #region Inner Classes
        private class Wpf32Window : System.Windows.Forms.IWin32Window
        {
            public IntPtr Handle { get; private set; }

            public Wpf32Window(Window wpfWindow)
            {
                Handle = new WindowInteropHelper(wpfWindow).Handle;
            }
        }
        #endregion
    }
}